package com.nowcoder.community.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author AnYang
 * @Description 切面测试类
 * @Version 1.0.0
 * @Date 2024/1/28
 */

//@Component
//@Aspect
public class AlphaAspect {

    /*
        around before
        before
        afterReturning
        after
        around after
     */

    // 先定义一个切点(切入点是：所有返回值 service下的所有bean的所有方法的所有参数)
    @Pointcut("execution(* com.nowcoder.community.service.*.*(..))")
    public void pointcut() {

    }

    // 在这个连接点之前
    @Before("pointcut()")
    public void before() {
        System.out.println("before");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("after");
    }

    // 在方法返回之后
    @AfterReturning("pointcut()")
    public void afterReturning() {
        System.out.println("afterReturning");
    }

    // 在方法抛出异常之后
    @AfterThrowing("pointcut()")
    public void afterThrowing() {
        System.out.println("afterThrowing");
    }

    // 在方法前和后
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around before");
        // 调用代理的方法
        Object obj = joinPoint.proceed();
        System.out.println("around after");
        return obj;
    }
}
